package za.org.jcicada.katcp;

/**
 * SensorBase.java
 *
 * @author Simon Cross
 * @author Adrianna Pinska
 *
 * Created on October 27, 2008, 17:15
 */

import za.org.jcicada.katcp.ISensor;
import za.org.jcicada.katcp.ISensorValue;
import za.org.jcicada.katcp.StrategyNone;

/**
 * An abstract base class for KAT device sensors.
 */
public abstract class SensorBase
implements ISensor {

    /**
     * The name of the sensor.
     */
    protected String name;

    /**
     * The sensor type.
     */
    protected ISensor.Type type;

    /**
     * A description of the sensor.
     */
    protected String description;

    /**
     * The units of the sensor value.
     */
    protected String units;

    /**
     * The sensor type parameters.
     */
    protected String[] typeParameters;

    /**
     * The current sensor value.
     */
    protected ISensorValue value;

    /**
     * The current sampling strategy.
     */
    protected ISamplingStrategy samplingStrategy;

    /**
     * Constructs a SensorBase instance.
     */
    public SensorBase() {
    }

    /**
     * Constructs a SensorBase instance.
     *
     * @param name  The sensor name.
     * @param type  The type of sensor.
     * @param initialValue  An initial value for the sensor.
     * @param description  A description of the sensor.
     * @param units   The units for the sensor value.
     * @param typeParameters   The sensor type parameters.
     */
    public SensorBase(String name, ISensor.Type type, ISensorValue initialValue,
                      String description, String units, String[] typeParameters) {
        init(name, type, initialValue, description, units, typeParameters);
    }

    /**
     * Initialise a sensor instance (if it was created without being initialised).
     *
     * @param name  The sensor name.
     * @param type  The type of sensor.
     * @param initialValue  An initial value for the sensor.
     * @param description  A description of the sensor.
     * @param units   The units for the sensor value.
     * @param typeParameters   The sensor type parameters.
     */
    protected void init(String name, ISensor.Type type, ISensorValue initialValue,
                      String description, String units, String[] typeParameters) {
        this.name = name;
        this.type = type;
        this.value = initialValue;
        this.description = description;
        this.units = units;
        if (typeParameters == null)
            this.typeParameters = new String [0];
        else
            this.typeParameters = (String []) (typeParameters.clone());
        samplingStrategy = new StrategyNone(new String[0]);
    }

    // ISensor Methods

    /**
     * Return the name of the sensor.
     *
     * @return  The sensor name.
     */
    public String getName() {
        return name;
    }

    /**
     * Return a description of the sensor.
     *
     * @return  A description of the sensor.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Return the sensor units in a form suitable for display next to the
     * sensor value.
     *
     * @return  The units for the sensor value.
     */
    public String getUnits() {
        return units;
    }

    /**
     * Return the sensor type.
     *
     * @return  The type of sensor.
     */
    public ISensor.Type getType() {
        return type;
    }

    /**
     * Return the type parameters.
     *
     * @return  Sensor parameters appropriate to the sensor type.
     */
    public String[] getParameters() {
        return typeParameters;
    }

    /**
     * Read the sensor and return an ISensorValue.
     *
     * @return  The current sensor value.
     */         
    public ISensorValue read() {
        return value;
    }

    /**
     * Set the sensor value.
     *
     * @param value  Set the current sensor value.
     */         
    public void set(ISensorValue value) {
        this.value = value;
    }

    /**
     * Format a sensor value of the same type as this ISensorType object.
     *
     * @param value  The value to format.
     * @return  KATCP string representation of the value as appropriate for
     *          this sensor type.
     */
    public abstract String formatValue(ISensorValue value);

    /**
     * Apply a change to the sensor sampling strategy.
     *
     * @param strategy  The new sampling strategy to use.
     * @see #getSampling
     */
    public void setSampling(ISamplingStrategy strategy) {
        samplingStrategy = strategy;
    } 
    
    /**
     * Return the current sampling strategy.
     *
     * @return strategy  The current sampling strategy for this sensor.
     * @see #setSampling
     */
    public ISamplingStrategy getSampling() {
        return samplingStrategy;
    }

}
